package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */

@Service
public class ShoppingCartServiceImpl  implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;



    @Transactional
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        //判断购物车数量的方法
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.get(shoppingCart);
        //表示查到了在购物车相同的商品
        if(shoppingCartList!=null  && shoppingCartList.size() !=0){
            shoppingCartList.get(0).setNumber(shoppingCartList.get(0).getNumber()+1);
            shoppingCartMapper.addNumber(shoppingCartList.get(0));
            return;
        }
        //表示没有查询到是该商品的第一次加入
        else{
            shoppingCart.setNumber(1);
        }
        //商品价格、名称、 图像 的存入

        //表示是否是菜品类
        if(shoppingCart.getDishId()!=null){
            Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
        }
        //表示是套餐类
        else{
            Setmeal setmeal = setmealMapper.getBYId(shoppingCartDTO.getSetmealId());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
        }

        //插入数据
            shoppingCartMapper.insert(shoppingCart);
            return;
    }

    @Override
    public List<ShoppingCart> getList() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCartMapper.getList(userId);
        return list;
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //如果购物车中该商品的数量大于一 ，则只减少相应一份数量
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.get(shoppingCart);
        //表示该商品数量是超过一份的
        if(shoppingCartList.get(0).getNumber() > 1){
            ShoppingCart cart = shoppingCartList.get(0);
            shoppingCartMapper.subNumber(shoppingCart);
            return;
        }
        //只是一份
        shoppingCartMapper.deleteOne(shoppingCart);
    }
}
